package com.raymond.pinyougou.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.raymond.pinyougou.entity.PageResult;
import com.raymond.pinyougou.entity.Result;
import com.raymond.pinyougou.pojo.TbBrand;
import com.raymond.pinyougou.sellergoods.service.BrandService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author Raymond
 * @Description //TODO
 * @Date 2019/3/22 23:41
 * @Version 1.0
 **/
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Reference
    private BrandService brandService;
    @RequestMapping("/findAll")
    public List<TbBrand> findAll() {
        return brandService.findAll();
    }

    /**
     * 分页查询
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(int pageIndex, int pageSize) {
        return brandService.findPage(pageIndex, pageSize);
    }

    /**
     * 新增品牌
     * @param brand
     * @return
     */
    @RequestMapping("/addBrand")
    public Result addBrand(@RequestBody TbBrand brand) {
        try {
            if(brandService.addBrand(brand)){
                return new Result(true, "新增成功");
            }
            return new Result(false, "新增失败");
        }catch (Exception e) {
            return new Result(false, "新增出错");
        }
    }

    /**
     * 通过ID查询品牌
     * @param id
     * @return
     */
    @RequestMapping("/getBrand")
    public TbBrand getBrand(long id) {
        return brandService.getBrand(id);
    }

    /**
     * 修改品牌
     * @param brand
     * @return
     */
    @RequestMapping("/updateBrand")
    public Result updateBrand(@RequestBody TbBrand brand) {
        try {
            if(brandService.updateBrand(brand)){
                return new Result(true, "修改成功");
            }
            return new Result(false, "修改失败");
        }catch (Exception e) {
            return new Result(false, "修改出错");
        }
    }

    /**
     * 删除多个品牌
     * @param ids
     * @return
     */
    @RequestMapping("delBrand")
    public Result delBrand(long[] ids) {
        try {
            if(brandService.deleteBrand(ids)){
                return new Result(true, "删除成功");
            }
            return new Result(false, "删除失败");
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除出错");
        }
    }

    /**
     * 条件查询+分页
     * @param brand
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/searchBrand")
    public PageResult searchBrand(@RequestBody TbBrand brand, int pageIndex, int pageSize) {
            return brandService.findPage(brand, pageIndex, pageSize);
    }

    /**
     * 规格下拉列表
     * @return
     */
    @RequestMapping("/selectOptionList")
    public List<Map> selectOptionList(){
        return brandService.selectOptionList();
    }
}
